let token = sessionStorage.getItem("token");
$(function () {
    let name = sessionStorage.getItem("username");
    $("#name").append(name);
    $("#leader").val(name);
    $("#leader_tel").val(sessionStorage.getItem("phone"));
    let video_list = [];
    let pic_list = [];
    let audio_list = [];
    let file_list = [];

    //设置select多选查询
    $("#jointlyers").selected({
        maxHeight: '200px',
        searchBox: '1'
    });
    //视频名称
    $('#doc-form-file').on('change', function () {
        let videoNames = '';
        $.each(this.files, function () {
            videoNames += '<span class="am-badge">' + this.name + '</span> ';
        });
        $('#file-list').html(videoNames);
    });
    //音频名称
    $('#audio').on('change', function () {
        let audioNames = '';
        $.each(this.files, function () {
            audioNames += '<span class="am-badge">' + this.name + '</span> ';
        });
        $('#audio-list').html(audioNames);
    });
    //文件名称
    $('#file_list').on('change', function () {
        let fileNames = '';
        $.each(this.files, function () {
            fileNames += '<span class="am-badge">' + this.name + '</span> ';
        });
        $('#allfile').html(fileNames);
    });
    // 提交
    $("#submit").click(function () {
        //表单验证
        var form1 = $('#form1');
        form1.validator({
            submit: function () {
                var formValidity = this.isFormValid();
                if (formValidity) {

                    // 图片上传
                    var formData = new FormData();
                    var pic = document.getElementById("file").files;

                    for (var j = 0; j < pic.length; j++) {
                        formData.append("" + j + "", pic[j]);
                    }
                    $.ajax({
                        url: testurl + "motion/file",
                        type: 'post',
                        headers: {"Authorization": "Bearer " + token},
                        data: formData,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (data) {
                            pic_list = data.obj;
                            console.log(data);
                        },
                        error: function (data) {
                            console.log(data);
                        }
                    });
                    //视频上传

                    formData = new FormData();
                    var pic2 = document.getElementById("doc-form-file").files;

                    for (let n = 0; n < pic2.length; n++) {
                        formData.append("" + n + "", pic2[n]);
                    }
                    $.ajax({
                        url: testurl + "motion/file",
                        type: 'post',
                        headers: {"Authorization": "Bearer " + token},
                        data: formData,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (data) {
                            video_list = data.obj;
                            console.log(data);
                        },
                        error: function (data) {
                            console.log(data);
                        }
                    });
                    // 音频上传
                    formData = new FormData();
                    var pic3 = document.getElementById("audio").files;

                    for (var q = 0; q < pic3.length; q++) {
                        formData.append("" + q + "", pic3[q]);
                    }
                    $.ajax({
                        url: testurl + "motion/file",
                        type: 'post',
                        headers: {"Authorization": "Bearer " + token},
                        data: formData,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (data) {
                            audio_list = data.obj;
                            console.log(data);
                        },
                        error: function (data) {
                            console.log(data);
                        }
                    });
                    // 文件上传
                    formData = new FormData();
                    var pic4 = document.getElementById("file_list").files;

                    for (var w = 0; w < pic4.length; w++) {
                        formData.append("" + w + "", pic4[w]);
                    }
                    $.ajax({
                        url: testurl + "motion/file",
                        type: 'post',
                        headers: {"Authorization": "Bearer " + token},
                        data: formData,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (data) {
                            file_list = data.obj;
                            console.log(data);
                        },
                        error: function (data) {
                            console.log(data);
                        }
                    });
                    let title = $("#title").val();
                    //获取select多个选中的值
                    //
                    // let    jointlyers = $("#jointlyers").val().toString();
                    // let leader_tel = $("#leader_tel").val();
                    let group_name = $("#group_name").val();
                    let commit_date = $("#commit_date").val();
                    let type = $("[name='type']:checked").val();
                    // let leader=$("#leader option:selected").val(); //获取选中的项
                    let link_addr = $("#link_addr").val();
                    let zip_code = $("#zip_code").val();
                    let content = $("#content").val();

                    let str = {
                        "title": title,
                        "group_name": group_name,
                        "commit_date": commit_date,
                        "nature": type,
                        "content": content,
                        "link_addr": link_addr,
                        "zip_code": zip_code,
                        "new_pic_list": pic_list,
                        "new_video_list": video_list,
                        "new_audio_list": audio_list,
                        "new_file_list": file_list
                    };
                    console.log(str);
                    $.ajax({
                        async: false,
                        headers: {"Authorization": "Bearer " + token},
                        type: "post",
                        url: testurl + "motion/",
                        contentType: "application/json",
                        data: JSON.stringify(str),
                        success: function (data) {
                            console.log(data);
                        }
                    });
                    return false
                } else {
                    return false
                }
                return false
            }
        });
    });

});